export const arrayMethods = Object.create(Array.prototype)

let methodsToPatch = [
  'push',
  'pop',
  'shift',
  'unshift',
  'splice',
  'reverse',
  'sort',
]

methodsToPatch.forEach((method) => {
  arrayMethods[method] = function (...args) {
    const result = Array.prototype[method].apply(this, args)

    const ob = this.__ob__

    let inserted
    switch (method) {
      case 'push':
      case 'unshift':
        inserted = args
        break
      case 'splice':
        inserted = args.slice(2)
        break
      default:
        break
    }

    if (inserted) ob.observeArray(inserted)

    ob.dep.notify()

    return result
  }
})
